package com.cy.pj.module.comtorller;

import com.cy.pj.module.pojo.RequestParameter;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

/*
*
* */
@RestController
public class ParmObjectContorller {//Handler

    @RequestMapping("/doHandleRequestParam01")
    public String doHandleRequestParam01
            (String name, @DateTimeFormat(pattern = "yyyy/MM/dd") @RequestParam(required = false) Date startDate, int num){
        return "request params name= "+name+"/startDate"+startDate+"/num="+num;
        //http://localhost:8080/doHandleRequestParam01?name=韦尚臻&startDate=2020/12/04
        //400一般是客户请求参数，与服务端参数处理规则不匹配
        //1）参数格式（例如日期格式 spring mvc默认可以处理的格式为yyyy/MM/dd HH:mm）
        //2) @RequestParam默认客户端必须要给它一个参数传值（required = true）
        //3)传的值与参数类型不匹配
        //4)数字格式不能为null---建议使用包装类型
    }

    /*
    * Rest风格--语法：/a/b/{c}/d/{e}, 其中{}内容为变量
    * 使用@PathVariable注解(pojo不可以一起用)
    * */
    @RequestMapping("/doHandleRequestParam02/{oid}/{code}")
    public String doHandleRequestParam02(@PathVariable("oid") Integer id,
                                         @PathVariable Integer code){
        return "request params id =" +id+"/code ="+code;
    }

    //==========================================================

    @RequestMapping("/doHandleRequestParam03")
    public String doHandleRequestParam03(RequestParameter pojo){
        return "rquest params "+pojo.toString();
    }

    @RequestMapping("/doHandleRequestParam04/{name}/{startDate}")
    public String doHandleRequestParam04(RequestParameter pojo){
        return "rquest params "+pojo.toString();
    }

    @RequestMapping("/doHandleRequestParam05")
    public String doHandleRequestParam05(@RequestParam Map<String,Object> map){
        return "rquest params "+map.toString();
    }

    @RequestMapping("/doHandleRequestParam06/{name}/{startDate}")
    public String doHandleRequestParam06(@PathVariable Map<String,Object> map){
        return "rquest params "+map.toString();
    }

}
